package Day26;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class Demo04 {
    //XPATH解析网络XML数据
    public static void main(String[] args) throws IOException, DocumentException {
        String phone = "18181145205";
        // 获取XML资源的输入流
        URL url = new URL("http://apis.juhe.cn/mobile/get?phone="+phone+"&dtype=xml&key=9f3923e8f87f1ea50ed4ec8c39cc9253");
        URLConnection conn = url.openConnection();
        InputStream is = conn.getInputStream();
        // 创建一个XML读取对象
        SAXReader sr = new SAXReader();
        // 通过读取对象 读取XML数据并返回文档
        Document doc = sr.read(is);

        Node n = doc.selectSingleNode("//company");
        System.out.println("运营商：" + n.getText());
    }
}
